package 日期.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateMain
{
	public static void main(String[] args)
	{	
		/*连接到键盘上面*/
		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "utf-8"));
			String line = "";
			while(true)
			{	
				System.out.println("=想停止按n=");
				line.trim();
				line = br.readLine();
				System.out.println("=您输入的时间是="+line);
				if("n".equalsIgnoreCase(line))
				{
					break ;
				}
				calendar(line);
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private static void calendar(String sDate)
	{
		/*实现一个日历测试类*/
		/*输入一个字符串,实现字符串格式化*/
		String pattern = "yyyy-"+"MM";
		/*格式化日期*/
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		int day = 0;
		int month = 0;
		try
		{
			/*赋值当前日期*/
			Date now = sdf.parse(sDate);
			//System.out.println("=="+now.toLocaleString());
			Calendar calend = Calendar.getInstance();
			/*把当前时间赋值个日历*/
			calend.setTime(now);
			/*获取当月的天数*/
			day=calend.getActualMaximum(Calendar.DATE);
			//System.out.println("day="+day);
			month=calend.get(Calendar.DAY_OF_WEEK);
			//System.out.println("month="+month);
		} catch (ParseException e)
		{
			//e.printStackTrace();
			System.out.println("=请正确输入时间格式如yyyy-MM(2000-11)=");
			return;
		}
		System.out.println("日\t一\t二\t三\t四\t五\t六");
		int count = 0 ;
		/*要想打印日历就要判断空格是从哪里开始的*/
		for (int i = 0; i < month-1; i++)
		{
			System.out.print(" \t");
			count ++ ;
		}
		/*循环一个月有多少天*/
		for (int i = 1; i <=day; i++)
		{
			System.out.print(i+"\t");
			count ++ ;
			if(count % 7==0)
			{
				System.out.println();
			}
		}
		/*最后一个位数换行*/
		System.out.println();
	}
}
